import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Widgets {
    private JFrame MainFrame;
    private JButton SaveButton;
    private JButton LoadButton;
    private JTextArea TextFrame;
    
    Widgets() {
        // Main window
        MainFrame = new JFrame("Simple text editor");
        MainFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        MainFrame.setSize(500, 300);
        MainFrame.setLocationRelativeTo(null); 
        MainFrame.setMinimumSize(new Dimension(500, 300));
        MainFrame.setMaximumSize(new Dimension(800, 500));  // doesn't work!!
        MainFrame.setPreferredSize(new Dimension(500, 300));
        MainFrame.setResizable(true);

        // base panel
        JPanel BasePanel = new JPanel();
        BasePanel.setLayout(new BoxLayout(BasePanel, BoxLayout.LINE_AXIS));

        // Text frame
        TextFrame = new JTextArea();
        JScrollPane scrollPane = new JScrollPane(TextFrame);
        scrollPane.setAlignmentY(Component.TOP_ALIGNMENT);
        BasePanel.add(scrollPane);

        // Right panel
        JPanel rightPanel = new JPanel();
        rightPanel.setLayout(new BoxLayout(rightPanel,BoxLayout.PAGE_AXIS));
        rightPanel.setAlignmentY(JPanel.TOP_ALIGNMENT);
        rightPanel.setBorder(BorderFactory.createEtchedBorder()); 
        rightPanel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));

        // Buttons
        Dimension BtnDim = new Dimension(80,25);
        SaveButton = new JButton("Save");SaveButton.setMinimumSize(BtnDim);
        SaveButton.setMaximumSize(BtnDim);
        SaveButton.setAlignmentY(JPanel.TOP_ALIGNMENT);

        LoadButton = new JButton("Load");LoadButton.setMinimumSize(BtnDim);
        LoadButton.setMaximumSize(BtnDim);
        LoadButton.setAlignmentY(JPanel.TOP_ALIGNMENT);
       
        rightPanel.add(SaveButton);
        rightPanel.add(Box.createRigidArea(new Dimension(0,5)));
        rightPanel.add(LoadButton);

        BasePanel.add(rightPanel);
        MainFrame.getContentPane().add(BasePanel);
    }

    public void SetSaveButtonEvent(ActionListener AL) {
        SaveButton.addActionListener(AL);
    }

    public void SetLoadButtonEvent(ActionListener AL) {
        LoadButton.addActionListener(AL);
    }

    public void SetCloseOperation(WindowListener WL) {
        MainFrame.addWindowListener(WL);
    }

    public String GetText() {
        return TextFrame.getText();
    }
    
    public void SetText(String t) {
        TextFrame.setText(t);
    }

    public void MainFrameShow() {
        SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    MainFrame.setVisible(true);
                }
            });
    }
}